#ifndef FILESOURCE_H
#define FILESOURCE_H

#include <stdio.h>
#include <atomic>
#include <string>
#include <stdio.h>
#include <thread>
#include <mutex>
#include <map>
#include <unistd.h>
#include "mk_media.h"
#include "mk_common.h"
#include "mk_h264_splitter.h"
#include "mk_mediakit.h"
#include "public.h"


class FileSource
{
public:
    FileSource();
    ~FileSource();

    //设置参数信息
    void setParam(const STU_CAMERAINFO &info);

    //任务控制
    void startTask();
    void stopTask();
    void autoReconnect();

    //媒体源操作
    void initMediaSource();
    void releaseMediaSource();

    // 打开文件
    AVFormatContext *openFile(const char* fileName);

    //线程执行体
    void run();

public:
    //mk相关接口
    mk_media m_media = nullptr;
    mk_h264_splitter m_splitter = nullptr;
    //设备参数
    STU_PUSHPARAM m_param;
    //文件视频格式为h264 否则h265
    bool m_encodeH264 = true;

    //运行状态 用于逻辑重连
    bool m_isRuning = false;
    //用于IO阻塞超时判断
    int64_t m_curIOTime = 0;
    //输入上下文
    AVFormatContext *m_inFmtCtx = nullptr;

    //h264裸流测试
    bool m_enableH264File = false;
    FILE *m_h264File = nullptr;
    uint32_t m_h264FileSize = 0;

    //视频文件参数
    int m_inputWidth = 1280;
    int m_inputHeight = 720;
    int m_inputFPS = 25;
    int m_inputFormat = 0;

private:
    //运行标志
    std::atomic<bool> m_bRun;

    //文件视频流标号
    int m_videoIndex = 0;

    //任务线程
    std::mutex m_mutex;
    std::thread *m_thread = nullptr;
};

#endif // FILESOURCE_H
